﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;

namespace SqlServerMaintenanceSolution.Models
{
    public class SqlServerInstance
    {
        private string MachineName { get; set; }
        private string InstanceName { get; set; }

        public string CompleteName { get; set; }
        public bool DefaultInstance { get; protected set; }

        public bool isDefaultInstance()
        {
            return DefaultInstance;
        }

        public SqlServerInstance()
        { }

        public SqlServerInstance(string machineName, string instanceName)
        {
            this.MachineName = machineName;
            this.InstanceName = instanceName;
            this.CompleteName = machineName + @"\" + instanceName;
            if (this.InstanceName == "MSSQLSERVER")
                this.DefaultInstance = true;
            else
                this.DefaultInstance = false;
        }

        public SqlServerInstance(string _sqlServerInstanceCompleteName)
        {
            this.MachineName = this.getMachineNameFromCompleteName(_sqlServerInstanceCompleteName);
            this.InstanceName = this.getInstanceNameFromCompleteName(_sqlServerInstanceCompleteName);
            this.CompleteName = _sqlServerInstanceCompleteName;
            if (this.InstanceName == "MSSQLSERVER")
                this.DefaultInstance = true;
            else
                this.DefaultInstance = false;
        }

        private string getMachineNameFromCompleteName(string completeName)
        {
            int indexOf = completeName.IndexOf(@"\");
            return completeName.Substring(0, indexOf);
        }

        private string getInstanceNameFromCompleteName(string completeName)
        {
            int indexOf = completeName.IndexOf(@"\") + 1;
            return completeName.Substring(indexOf, completeName.Length - indexOf);
        }

        /// <summary>
        /// Gets the installed Sql Server instances on the running machine.
        /// </summary>
        /// <returns>A list of Sql Server Instance.</returns>
        public static IEnumerable<SqlServerInstance> GetInstalledInstances()
        {
            var installedInstances = new List<SqlServerInstance>();

            RegistryView registryView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
            using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView))
            {
                RegistryKey instanceKey = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL", false);
                if (instanceKey != null)
                {
                    foreach (var instanceName in instanceKey.GetValueNames())
                    {
                        installedInstances.Add(new SqlServerInstance(Environment.MachineName, instanceName));
                    }
                }
            }

            return installedInstances;
        }
    }
}